//数据库的名是"mydatebase.db"
var sqlite3 = require("sqlite3");

//创建数据库
var database = new sqlite3.Database("mydatebase.db", function (err) {
    if (err) {
        console.log("new database error,", err.message);
    } else {
        console.log("new database success");
    }
});

async function add(table,arr) {
    database.run(`insert into t_${table} values(?,?,?)`, arr, err => {
        let msg = err ? 'error' : 'success'
        console.log('insert ' + msg)
    })

}
async function del(table,commonSql) {
    database.run(`delete from t_${table} where ${commonSql}`, err => {
        let msg = err ? 'error' : 'success'
        console.log('delete ' + msg)
    })

}
function query(table,commonSql = '1=1') {

    return new Promise(resolve => {
        database.all(`select * from t_${table} where ${commonSql}`, (err, rows) => {
            let msg = err ? 'error' : 'success'
            console.log('query ' + msg)
            resolve(rows)
        })
    })

}

module.exports = {
    add, query,del
}

